import tkinter as tk
from tkinter import ttk
from scitk.widgets.paradialog import ParaDialogFrame
from node import Node

class SettingsFrame(ttk.Frame):
    def __init__(self,parent):
        super().__init__(parent)
        self.node = None
        self.dialog_frame = None
        self.refresh_button  = None
        self.layout()
        
    def layout(self):
        if self.dialog_frame is not None:
            self.dialog_frame.destroy()
            self.refresh_button.destroy()
        self.dialog_frame = ParaDialogFrame(self,'Test')
        self.dialog_frame.pack(fill=tk.BOTH, expand = True)
        self.refresh_button = ttk.Button(self, text = 'refresh', command = self.refresh)
        self.refresh_button.pack()
        
    def refresh(self):
        self.set_node_properties(self.node)
        print(self.node.get_property_dic())
        self.node.refresh_block_look()
        #self.set_node_info(Node('bccccccccccc',8 ,inputVarNames=['input1','input2','input23'],outputVarNames = ['output']))
        
    def set_node_properties(self,node):
        if self.dialog_frame is not None:
            params_dic = self.dialog_frame.para
            for k in params_dic.keys():
                l = k.split(' ')
                if l[0].startswith(('input','output')):# 更改接口名称
                    port_id = int(l[1][1:])
                    if l[0].startswith('input'):
                        node.input_ports[port_id-1].name = params_dic[k]
                    else:
                        node.output_ports[port_id-1].name = params_dic[k]
                elif l[0]=='function':
                    node.func_name = params_dic[k]
                elif l[0].startswith( ('id','name')):
                    setattr(node,l[0][:-1],params_dic[k])
                                       
    def set_node_info(self,node):
        self.layout()
        # 每次设置node信息的时候，都要刷新一遍。由于各种设置项实在太多，一项一项删除太麻烦，
        #所以干脆就直接这么搞了。
        self.node = node
        property_dic = node.get_property_dic()
        nodeInfoList = []
        nodeInfoDic = {}
        
        node_name = 'name:'
        nodeInfoDic[node_name] = property_dic['name']
        nodeInfoList.append((str,node_name, node_name,''))     

        node_name = 'id:'
        nodeInfoDic[node_name] = property_dic['id']
        nodeInfoList.append((int,node_name, (0,1000), 0,  node_name,''))   #    (int, 'age', (0,150), 0, 'age', 'years old'),

        for i,iportName in enumerate(property_dic['input']):
            tag = 'input #%d name:'%(i+1)
            nodeInfoDic[tag] = iportName
            nodeInfoList.append((str,tag,tag,''))
            
        for i,oportName in enumerate(property_dic['output']):
            name = 'output #%d name:'%(i+1)
            nodeInfoDic[name] = oportName
            nodeInfoList.append((str,name, name,''))
        
        func_name = 'function name:'
        nodeInfoDic[func_name] = property_dic['func']
        nodeInfoList.append((str,func_name, func_name,''))  
            
        print(nodeInfoList)
        print(nodeInfoDic)
        self.dialog_frame. init_view(nodeInfoList, nodeInfoDic, preview=True, modal=False)
        pass

if __name__== '__main__':
##        para = {'name':'yxdragon', 'age':10, 'h':1.72, 'w':70, 'sport':True, 'sys':'Mac', 'lan':['C/C++', 'Python'], 'c':(255,0,0)} 
##
##    view = [('lab', 'lab', 'This is a questionnaire'),
##            (str, 'name', 'name', 'please'), 
##            (int, 'age', (0,150), 0, 'age', 'years old'),
##            (float, 'h', (0.3, 2.5), 2, 'height', 'm'),
##            ('slide', 'w', (1, 150), 0, 'weight','kg'),
##            (bool, 'sport', 'do you like sport'),
##            (list, 'sys', ['Windows','Mac','Linux'], str, 'favourite', 'system'),
##            ('chos', 'lan', ['C/C++','Java','Python'], 'lanuage you like(multi)'),
##            ('color', 'c', 'which', 'you like')]
##    para = {'input_1_name':'input1', 'input_2_name':'input2'}
##    view = [('lab','lab','Node Properties'),
##            (str,'input_1_name','name','please'),
##            (str,'input_2_name','name','please'),
##        ]
    n = Node('b',8 ,inputVarNames=['input1','input2','input23'],outputVarNames = ['output'])
    app = tk.Tk()
    pd = SettingsFrame(app)
    pd.pack(expand =1, fill = tk.BOTH)
    pd.set_node_info(n)
    app.mainloop()
        
        